package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.HrEntity;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.service.IHrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class HrController {
    @Autowired
    private IHrService iHrService;
    @RequestMapping("/admin/hr/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, HrEntity hrEntity, HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        hrEntity.setStatus(Tool.stringToInt(status));
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        hrEntity.setPageEntity(pageEntity);
        List<HrEntity> list = iHrService.findAll(hrEntity);
        int count = iHrService.getCount(hrEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/hr/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp, HrEntity hrEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        HrEntity model = iHrService.findById(hrEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/hr/add")
    @ResponseBody
    public void add(HttpServletResponse resp, HrEntity hrEntity,HttpServletRequest req) throws IOException {
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        hrEntity.setTitle((String) req.getAttribute("title"));
        hrEntity.setHref((String) req.getAttribute("href"));
        hrEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        hrEntity.setImg((String) req.getAttribute("img"));
        hrEntity.setContentOne((String) req.getAttribute("contentOne"));
        hrEntity.setContentTwo((String) req.getAttribute("contentTwo"));
        String result = iHrService.add(hrEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/hr/update")
    @ResponseBody
    public void update(HttpServletResponse resp, HrEntity hrEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        hrEntity.setId(Tool.stringToInt((String) req.getAttribute("id")));
        hrEntity.setTitle((String) req.getAttribute("title"));
        hrEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        hrEntity.setImg((String) req.getAttribute("img"));
        hrEntity.setContentOne((String) req.getAttribute("contentOne"));
        hrEntity.setContentTwo((String) req.getAttribute("contentTwo"));
        hrEntity.setHref((String) req.getAttribute("href"));
        String result = iHrService.update(hrEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/hr/del")
    @ResponseBody
    public void del(HttpServletResponse resp, HrEntity hrEntity) throws IOException {
        int result = iHrService.del(hrEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
//    @RequestMapping("/hr")
//    @ResponseBody
//    public void getHrModelListForHome(HttpServletResponse resp,HttpServletRequest req) throws IOException {
//        Tool.setEncoding(req,resp);
//        List<HrEntity> list = iHrService.getHrModelListForHome();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("list",list);
//        resp.getWriter().println(jsonObject);
//    }
}
